﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace CreditCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintPayments(CalculatePaymentDates(SelectDate(), SelectNuOfPayments()));            
            Console.ReadKey();
        }

        static int SelectNuOfPayments()
        { 
            Console.WriteLine("Please enter the number of payments: ");
            int NumberOfPayment = Int32.Parse(Console.ReadLine());
            return NumberOfPayment;
        }

        static DateTime SelectDate()
        {
            DateTime DateToTakeCredit = new DateTime();
            CultureInfo MyCultureInfo = new CultureInfo("");
            Console.WriteLine("Please enter 1 if your culture UKR or 2 if your culture ENG");
            int CultureFlag = Int32.Parse(Console.ReadLine());
            if (CultureFlag == 2)
                MyCultureInfo = new CultureInfo("en-US");
            else if (CultureFlag == 1)
                MyCultureInfo = new CultureInfo("uk-UA");
            else Console.WriteLine("incorrect input");

            
            Console.WriteLine("If you want to select today's date, enter 1. If you want to select another date enter 2.");
            int DateFlag = Int32.Parse(Console.ReadLine());
            if (DateFlag == 1)
                DateToTakeCredit = DateTime.Now;
            else if (DateFlag == 2)
            {
                Console.Write("Please enter day: ");
                string day = Console.ReadLine();
                Console.Write("Please enter month: ");
                string month = Console.ReadLine();
                Console.Write("Please enter year: ");
                string year = Console.ReadLine();
                string Date = day + " " + month + " " + year;
                DateToTakeCredit = DateTime.Parse(Date, MyCultureInfo);
            }
            else Console.WriteLine("incorrect input");

            return DateToTakeCredit;
        }       

        static DateTime[] CalculatePaymentDates(DateTime DateOfTakingСredit, int NumOfPayments)
        {
            DateTime[] CreditData = new DateTime[NumOfPayments];
            for (int i = 0; i < NumOfPayments; i++)
            {
                CreditData[i] = DateOfTakingСredit.AddMonths(i + 1);

                if (CreditData[i].DayOfWeek == DayOfWeek.Saturday)
                    CreditData[i] = CreditData[i].AddDays(-1);
                if (CreditData[i].DayOfWeek == DayOfWeek.Sunday)
                    CreditData[i] = CreditData[i].AddDays(-2);
            }
                return CreditData;
        }

        static void PrintPayments(DateTime[] PaymentSchedule)
        {
            for (int i = 0; i < PaymentSchedule.Length; i++)
            {
                Console.WriteLine("Payment №: {0}     Date of payment: {1}", i + 1, PaymentSchedule[i]);
            }
        }
    }
}
